<template>
  <div>
    <Header v-if="shows == 'list'">
      <el-button type="primary" size="small" @click="add">
        新建砸金蛋
      </el-button>
      <!-- <div class='list-heads-right'>
              <el-input placeholder='请输入名称'
                        v-model='title'
                        size='small'>
                <el-button slot='append'
                           icon='el-icon-search'
                           @click='getList'></el-button>
              </el-input>
            </div> -->
    </Header>
    <Main
      v-if="shows == 'list'"
      :data="listData"
      :footer-lock="totals > 10"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="活动名称"
        align="center"
        property="activity_name"
      ></el-table-column>
      <el-table-column label="抽奖次数" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.partake_type == 1"
            >一天{{ scope.row.partake_type_content }}次</span
          >
          <span v-if="scope.row.partake_type == 2"
            >一人{{ scope.row.partake_type_content }}次</span
          >
          /
          <span v-if="scope.row.activity_type == 1">无限制抽签</span>
          <span v-if="scope.row.activity_type == 2"
            >每次消耗{{ scope.row.activity_type_content }}积分</span
          >
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" property="authority">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0">未开始</span>
          <span v-if="scope.row.status == 1">进行中</span>
          <span v-if="scope.row.status == 2">结束</span>
          <span v-if="scope.row.status == 3">停用</span>
          <span v-if="scope.row.status == 4">启用</span>
        </template>
      </el-table-column>
      <el-table-column label="活动时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.begin_time }}至{{ scope.row.end_time }}
        </template>
      </el-table-column>
      <el-table-column label="抽奖人数" align="center">
        <template slot-scope="scope">
          {{ scope.row.nums }}人
        </template>
      </el-table-column>
      <el-table-column label="中奖人数" align="center">
        <template slot-scope="scope">
          {{ scope.row.winning_nums }}人
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        width="250"
        class-name="tableoperation"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="edit(scope.row.id)">
            编辑
          </el-button>
          <el-button size="mini" type="text" @click="win(scope.row.id)">
            中奖名单
          </el-button>
          <el-button size="mini" type="text" @click="del(scope.row.id)">
            结束
          </el-button>
        </template>
      </el-table-column>
    </Main>
    <Footer :total="totals">
      <el-pagination
        :page-size="10"
        layout="prev, pager, next"
        :total="totals"
        :current-page="pages"
        @current-change="changes"
      >
      </el-pagination>
    </Footer>

    <Add v-if="shows == 'add'" :activity-id="activityId" @out="out" />

    <winList v-if="shows == 'winList'" :activity-id="activityId" @out="out" />
  </div>
</template>
<script>
import { ACTIVITY_LIST, END_PRIZE } from "../service";

export default {
  components: {
    Add: () => import("./form"),
    winList: () => import("./winList")
  },
  data() {
    return {
      shows: "list",
      listData: [],
      totals: 0,
      pages: 1,
      activityId: "",
      title: ""
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async del(activityId) {
      await this.$confirm("是否结束该活动?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          END_PRIZE({ activity_id: activityId, status: 2 }).then(
            ({ code, msg }) => {
              if (code === 200) {
                this.getList();
              }
              this.$message({
                message: msg,
                type: code === 200 ? "success" : "error"
              });
            }
          );
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除!"
          });
        });
    },
    async getList() {
      await ACTIVITY_LIST({
        page: this.pages,
        pageshow: 10
      }).then(({ code, data, msg }) => {
        if (code === 200) {
          this.listData = data.list;
          this.totals = data.count;
        } else {
          this.$message.error(msg);
          this.listData = [];
        }
      });
    },
    add() {
      this.shows = "add";
    },
    edit(activityId) {
      this.activityId = activityId;
      this.shows = "add";
    },
    win(activityId) {
      this.activityId = activityId;
      this.shows = "winList";
    },
    out() {
      this.shows = "list";
      this.activityId = "";
      this.getList();
    },
    changes(page) {
      this.pages = page;
      this.getList();
    }
  }
};
</script>
